package com.mycompany.goodsys.basecom.Observer;

import java.util.HashMap;

public class EventSystem {
    private static HashMap<String,EventAction> event_dict = new HashMap<String, EventAction>();

    static{
        BindEvent(EventName.ExitApplication,(Object[] objs)->{
            Clear();
        });
    }

    public static void BindEvent(String event_name,EventAction event_action){
        if(event_name == null || event_name == ""){
            System.out.println("添加了一个空事件");
            return;
        }
        if(event_dict.containsKey(event_name)){
            System.out.println("已经包含有相关的事件了! event_name: "  + event_name);
        }
        else{
            event_dict.put(event_name,event_action);
        }
    }

    public static void RemoveEvent(String event_name){
        if(event_name == null || event_name == ""){
            System.out.println("移除空事件名是空的");
            return;
        }

        if(event_dict.containsKey(event_name)){
            event_dict.remove(event_name);
        }
    }

    private static void Clear(){
        event_dict.clear();
    }

    public static void Invoke(String event_name,Object... objs){
        if(event_name == null || event_name == ""){
            return;
        }

        if(event_dict.containsKey(event_name)){
            EventAction action = event_dict.get(event_name);
            try{
                action.CallFunc(objs);
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
    }
}